﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Workflows
{
    public class StageProperties
    {
        Dictionary<string, object> _dict;

        public StageProperties()
        {

        }

        public int Count
        {
            get
            {
                if (_dict != null)
                    return _dict.Count;
                else
                    return 0;
            }
        }

        private Dictionary<string, object> EnsureDictionary()
        {
            if (_dict == null)
                _dict = new Dictionary<string, object>();
            return _dict;
        }

        public bool IsDefined(string name)
        {
            if (_dict == null)
                return false;
            return _dict.ContainsKey(name);
        }

        public object this[string name]
        {
            get
            {
                if (_dict == null)
                    return null;

                object v;
                if (_dict.TryGetValue(name, out v))
                    return v;
                else
                    return null;

            }
            set
            {
                EnsureDictionary()[name] = value;
            }
        }

        public T GetValueOrDefault<T>(string name, T defaultValue)
        {
            if (IsDefined(name))
                return (T)this[name];
            else
                return defaultValue;
            
        }

        public T GetValueOrDefault<T>(string name)
        {
            return GetValueOrDefault<T>(name, default(T));
        }
    }
}
